package com.platform.tony.controller;


import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSONObject;
import com.platform.tony.common.Constant;
import com.platform.tony.config.EmailAuthToken;
import com.platform.tony.config.PhoneAuthToken;
import com.platform.tony.config.WeChatAuthToken;
import com.platform.tony.dto.LoginDto;
import com.platform.tony.utils.IdUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.web.access.ExceptionTranslationFilter;
import org.springframework.security.web.access.intercept.FilterSecurityInterceptor;
import org.springframework.security.web.authentication.AbstractAuthenticationProcessingFilter;
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;
import org.springframework.security.web.context.SecurityContextPersistenceFilter;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.net.InetAddress;
import java.net.UnknownHostException;
import java.time.Duration;

@Slf4j
@RestController
@RequestMapping(value = "api")
public class HomeController {

    @Autowired
    private AuthenticationManager manager;

    @Autowired
    private StringRedisTemplate template;

    /**
     * 发送邮箱验证码
     */
    @GetMapping(value = "email/code")
    public String createEmailCode(String email) {
        String code = IdUtils.code();
        log.info("email {} and code {}", email, code);
        template.opsForValue().set(Constant.EMAIL_CODE_PREFIX + email, code);
        template.expire(Constant.EMAIL_CODE_PREFIX + email, Duration.ofSeconds(300));
        JSONObject json = new JSONObject();
        json.put("code", code);
        json.put("email", email);
        return json.toString();
    }
    /**
     * 发送手机验证码
     */
    @GetMapping(value = "phone/code")
    public String createPhoneCode(String phone) {
        String code = IdUtils.code();
        log.info("phone {} and code {}", phone, code);
        template.opsForValue().set(Constant.PHONE_CODE_PREFIX + phone, code);
        template.expire(Constant.PHONE_CODE_PREFIX + phone, Duration.ofSeconds(300));
        JSONObject json = new JSONObject();
        json.put("code", code);
        json.put("phone", phone);
        return json.toString();
    }

    @PostMapping(value = "auth")
    public String login(@RequestBody LoginDto login) {

        // 验证码
        String code = StringUtils.trimToEmpty(login.getCode());
        String type = login.getType();
        Authentication authenticate = null;
        if (StrUtil.equals("1", type)) {
            PhoneAuthToken token = new PhoneAuthToken(login.getCellphone(), login.getCode());
            authenticate = manager.authenticate(token);
        }
        if (StrUtil.equals("2", type)) {
            EmailAuthToken token = new EmailAuthToken(login.getEmail());
            authenticate = manager.authenticate(token);
        }
        if (StrUtil.equals("3", type)) {
            UsernamePasswordAuthenticationToken tk = new UsernamePasswordAuthenticationToken(null,null,null);
            UsernamePasswordAuthenticationToken token =
                    new UsernamePasswordAuthenticationToken(login.getUsername(), login.getPassword());
            authenticate = manager.authenticate(token);
        }

        log.info("authenticate is {}", JSONObject.toJSONString(authenticate));
        return "home";

    }


    @GetMapping(value = "/home")
    public String home() {

        InetAddress serverHost = null;
        try {
            serverHost = InetAddress.getLocalHost();
            log.info("hostname is {} and address {}", serverHost.getHostName(), serverHost.getHostAddress());
        } catch (UnknownHostException e) {
            e.printStackTrace();
        }

        return "success";
    }


}
